﻿using System.Windows;
using System.Windows.Input;

namespace SmartSpace.UI.Silverlight.Dynamic.Controls
{
    /// <summary>
    /// 文本框控件
    /// </summary>
    public partial class EditorControl : PMFDataControl
    {
        public EditorControl()
        {
            InitializeComponent();
        }

        #region DependencyProperty

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedDictData.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(EditorControl), new PropertyMetadata(default(string), OnTextPropertyChangedCallback));

        static void OnTextPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            EditorControl control = d as EditorControl;
            control.m_editor.Content = (string)e.NewValue;
        }

        public bool IsReadOnly
        {
            get { return (bool)GetValue(IsReadOnlyProperty); }
            set { SetValue(IsReadOnlyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemsSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsReadOnlyProperty =
            DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(EditorControl), new PropertyMetadata(default(bool), OnIsReadOnlyPropertyChangedCallback));

        static void OnIsReadOnlyPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            EditorControl ctrl = d as EditorControl;
            ctrl.SetReadOnly((bool)e.NewValue);
        }

        #endregion

        #region Overrides

        public override void UpdateSource()
        {
            this.Text = m_editor.Content;
            this.GetBindingExpression(EditorControl.TextProperty).UpdateSource();
        }

        private void _ctlMessage_MouseEnter(object sender, MouseEventArgs e)
        {
            _ctlMessage.Visibility = System.Windows.Visibility.Collapsed;
        }

        #endregion

        private void SetReadOnly(bool isReadOnly)
        {
            if (isReadOnly)
            {
               m_editor.IsReadOnly = true;
            }
            else
            {
                m_editor.IsReadOnly = false;
            }
        }
    }
}
